package pl.s7n.paint;

import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.UndoManager;

import pl.s7n.paint.utils.UpdatableGUI;

public class CanvasUndoManager extends UndoManager {
  private static final long serialVersionUID = -6532448591886960098L;

  UpdatableGUI              parent;

  public CanvasUndoManager(UpdatableGUI parent) {
    this.parent = parent;
  }

  @Override
  public void redo() {
    super.redo();
    updateGui();
  }

  @Override
  public void undo() {
    super.undo();
    updateGui();
  }

  @Override
  public void undoableEditHappened(UndoableEditEvent e) {
    super.undoableEditHappened(e);
    updateGui();
  }

  @Override
  public synchronized void discardAllEdits() {
    super.discardAllEdits();
    updateGui();
  };

  private void updateGui() {
    if (parent != null) {
      parent.updateGUI();
    }
  }
}
